#include "ImaService.h"
#include <iostream>
#include <stdio.h>
#include <axutil_env.h>
#include <Environment.h>
#include <OMText.h>
#include "ImaServiceConstants.h"
#include "ImaTcpRequestMaker.h"
#include "ImaConstants.h"
#include "ImaServiceUtils.h"
#include "ImaActiveUsers.h"
#include <vector>
 
/** Load the service into axis2 engine */
WSF_SERVICE_INIT(ImaService)

OMElement* ImaService::invoke(OMElement *ele, MessageContext *msgCtx)
{
	LOG(debug, __FILE__, __LINE__, "ImaService::invoke called");

	string localName = ele->getLocalname();

	char log_msg[1024];
	sprintf(log_msg, "localname is %s", localName.c_str());
	LOG(debug, __FILE__, __LINE__, log_msg);
	LOG(debug, __FILE__, __LINE__, "The xml request packet received from the client:");
	LOG(debug, __FILE__, __LINE__, ele->toString().c_str());
	printf("The xml packet received from client is %s\n", ele->toString().c_str());

	OMElement* pEle = NULL;

	if((strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_REGISTERUSER) == 0)|| (strcmp(localName.c_str(), "registerUser") == 0)) {
		pEle = registerUser(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_CONFIRMUSERREGISTRATION) == 0) {
		return confirmUserRegistration(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_UPDATEUSERDETAILS) == 0) {
		return updateUserDetails(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_CHANGEPASSWORD) == 0) {
		return changePassword(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_FORGOTPASSWORD) == 0) {
		return forgotPassword(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_LOGIN) == 0) {
		return login(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_LOGOUT) == 0) {
		return logout(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_ADDINFO) == 0) {
		return addInfo(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_UPDATEINFO) == 0) {
		return updateInfo(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_DELETEINFO) == 0) {
		return deleteInfo(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_ADDINFORECIPIENTS) == 0) {
		return addInfoRecipients(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_DELETEINFORECIPIENT) == 0) {
		return deleteInfoRecipient(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_ADDRECIPIENT) == 0) {
		return addRecipient(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_UPDATERECIPIENT) == 0) {
		return updateRecipient(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_DELETERECIPIENT) == 0) {
		return deleteRecipient(ele, msgCtx);
	} else if(strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_FETCHUSERDATA) == 0) {
		return fetchUserData(ele, msgCtx);
	} else if (strcmp(localName.c_str(), IMA_SERVICE_XML_TAG_USERFEEDBACK) == 0) {
		return userFeedback(ele, msgCtx);
	}

	LOG(debug, __FILE__, __LINE__, "returning from invoke");
	return pEle;
}

OMElement* ImaService::onFault(OMElement *ele)
{
	printf("ImaService::onFault called\n");
	LOG(error, __FILE__, __LINE__, "ImaService::onFault called");
	OMElement *responseEle = new OMElement("ImaServiceServiceErrorResponse");
	responseEle->setText("ImaService Service Failed");
	return responseEle;
}

void ImaService::init() {
	printf("ImaService::init called\n");

	char *pWsfCppHomeEnv = getenv("WSFCPP_HOME");

	if(pWsfCppHomeEnv == NULL) {
		printf("Please set the value of the environment variable WSFCPP_HOME first\n");
		exit(-1);
	}

	char logConfFile[1024];
	sprintf(logConfFile, "%s/services/ima/log4cpp.properties", pWsfCppHomeEnv);

	gpLogger = ImaLogger::getInstance(logConfFile);

	if(gpLogger == NULL) {
		printf("Failed to initialize the logger\n");
		exit(-1);
	}

	char log_msg[1024];
	sprintf(log_msg, "Logger initialized");
	LOG(debug, __FILE__, __LINE__, log_msg);

   /* Accessing of the Config file, if the config file exists then parse the config file */
	char serviceConfFile[1024];
	sprintf(serviceConfFile, "%s/services/ima/ima_serv_conf.json", pWsfCppHomeEnv);
   if(access(serviceConfFile, R_OK) != 0) {
      LOG(error, __FILE__, __LINE__, "Conf file ima_serv_conf.json doesn't exist");
      exit(EXIT_FAILURE);
   }

   gpConf = new ImaConf(serviceConfFile);
   if(gpConf->parseConfFile() != 0) {
      LOG(error, __FILE__, __LINE__, "Failed to parse conf file ima_serv_conf.json");
      exit(EXIT_FAILURE);
   }
}
 
//OMElement* ImaService::greet(OMElement* inMsg)
//{
//	string greet;
//	if(inMsg)
//	{	
//		cout << "greet invoked" << endl;
////		try
//		{	
//			if(inMsg->getFirstChild() && inMsg->getFirstChild()->nodeType() == AXIOM_TEXT)
//			{
//				OMText *text = dynamic_cast<OMText*>(inMsg->getFirstChild());
//				greet = text->getValue();
//				cout << "Client greeted Saying" << greet << endl;				
//			}
//		}/*catch (bad_cast) 
//		{
//			return NULL;
//		} */
//		OMElement *helloEle = new OMElement("greetResponse");
//		OMElement *text = new OMElement("text");
//		helloEle->setText(greet);
//		return helloEle;
//	}
//	return NULL;
//}


//OMElement* ImaService::greet(OMElement* inMsg, MessageContext* pMsgCtx)
//{
//	printf("ImaService::greet called\n");
//	string greet;
//	if(inMsg)
//	{	
//		cout << "greet invoked" << endl;
//		//greet = "ImaService Kamal";
////		cout << "Client greeted Saying" << greet << endl;				
////		try
//		{	
//			if(inMsg->getFirstChild() && inMsg->getFirstChild()->nodeType() == AXIOM_TEXT)
//			{
//				cout << "first child found" << endl;
//				OMText *text = dynamic_cast<OMText*>(inMsg->getFirstChild());
//				greet = text->getValue();
//				cout << "Client greeted Saying" << greet << endl;				
//			}
//		}/*catch (bad_cast) 
//		{
//			return NULL;
//		} */
//		OMElement *helloEle = new OMElement("greetResponse");
//		OMElement *text = new OMElement("text");
//		helloEle->addChild(text);
//		text->setText(greet);
//		return helloEle;
//	}
//	return NULL;
//}


//OMElement* ImaService::registerUser(OMElement* inMsg, MessageContext* pMsgCtx) {
//	printf("ImaService::register called\n");
//	string greet;
//	if(inMsg)
//	{	
//		cout << "register invoked" << endl;
//		//greet = "ImaService Kamal";
////		cout << "Client greeted Saying" << greet << endl;				
////		try
//		{	
//			if(inMsg->getFirstChild() && inMsg->getFirstChild()->nodeType() == AXIOM_TEXT)
//			{
//				cout << "first child found" << endl;
//				OMText *text = dynamic_cast<OMText*>(inMsg->getFirstChild());
//				greet = text->getValue();
//				cout << "Client greeted Saying" << greet << endl;				
//			}
//		}/*catch (bad_cast) 
//		{
//			return NULL;
//		} */
//		OMElement *helloEle = new OMElement("greetResponse");
//		OMElement *text = new OMElement("text");
//		helloEle->addChild(text);
//		text->setText(greet);
//		return helloEle;
//	}
//	return NULL;
//}

OMElement* ImaService::createChildElement(OMElement*& pParent, const char* pName, const long& value) {
	char log_msg[1024];
	sprintf(log_msg, "child element name: %s, child element value: %ld", pName, value);
	LOG(debug, __FILE__, __LINE__, log_msg);

	OMElement* pChildElement = new OMElement(pName);
	char tempStr[256];
	sprintf(tempStr, "%ld", value);
	pChildElement->setText(tempStr);
	bool bRetVal = pParent->addChild(pChildElement);
	if(!bRetVal) {
		printf("Failed to addChild");
	}

	return pChildElement;
}

OMElement* ImaService::createChildElement(OMElement*& pParent, const char* pName, const char* pText) {
	LOG(debug, __FILE__, __LINE__, "ImaService::createChildElement called");
	char log_msg[1024];
	sprintf(log_msg, "child element name: %s, child element text: %s", pName, pText);
	LOG(debug, __FILE__, __LINE__, log_msg);

	OMElement* pChildElement = new OMElement(pName);
	if(pText) {
		pChildElement->setText(pText);
	}
	bool bRetVal = pParent->addChild(pChildElement);
	if(!bRetVal) {
		printf("Failed to addChild");
	}

	return pChildElement;
}

OMAttribute* ImaService::addAttribute(const char* pAttribName, const std::string& strAttribVal, OMElement* pEle) {
	char log_msg[1024];
	sprintf(log_msg, "ImaService::addAttribute called with attribute name %s and value %s", pAttribName, strAttribVal.c_str());
	LOG(debug, __FILE__, __LINE__, log_msg);

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(pAttribName, strAttribVal, ns);
	bool bRetVal = pEle->addAttribute(pAttrib); //TODO: uncomment this

	return pAttrib;
}

std::string ImaService::getElementText(OMElement* pElement) {
	LOG(debug, __FILE__, __LINE__, "ImaService::getElementText called");

	std::string strText = "";
	if(pElement && pElement->getFirstChild() && pElement->getFirstChild()->nodeType() == AXIOM_TEXT) {
		OMText* pText = dynamic_cast<OMText*>(pElement->getFirstChild());
		strText = pText->getValue();
	}

	char log_msg[1024];
	sprintf(log_msg, "element text is %s", strText.c_str());
	LOG(debug, __FILE__, __LINE__, log_msg);
	return strText;
}

int ImaService::getJsonIntValue(string& jsonString, const char* pKey, long& value) {
	LOG(debug, __FILE__, __LINE__, "ImaService::getJsonIntValue called");

	int retVal= -1;
	json_t* pRoot = NULL;
	json_error_t error;
	pRoot = json_loads(jsonString.c_str(), 0, &error);
	if(pRoot) {
		json_t* pJsonObj = json_object_get(pRoot, pKey);
		if((pJsonObj != NULL) && (json_is_integer(pJsonObj))) {
			retVal = 0;
			value = json_integer_value(pJsonObj);
			char log_msg[1024];
			sprintf(log_msg, "value is %ld", value);
			LOG(debug, __FILE__, __LINE__, log_msg);
		}
	}
	
	json_decref(pRoot);
	return retVal;	
}

int ImaService::getJsonIntValue(const json_t* pRoot, const char* pKey, long& value) {
	LOG(debug, __FILE__, __LINE__, "ImaService::getJsonIntValue called");
	if(pRoot == NULL) {
		LOG(error, __FILE__, __LINE__, "JSON object is NULL");
		return -1;
	}

	if(pKey  == NULL) {
		LOG(error, __FILE__, __LINE__, "JSON Key to be searched is NULL");
		return -1;
	}

	int retVal = -1;
	
	json_t* pJsonObj = json_object_get(pRoot, pKey);
	if((pJsonObj != NULL) && (json_is_integer(pJsonObj))) {
		retVal = 0;
		value = json_integer_value(pJsonObj);
		char log_msg[1024];
		sprintf(log_msg, "value is %ld", value);
		LOG(debug, __FILE__, __LINE__, log_msg);
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Proper integer value for %s not found in the JSON object.", pKey);
		LOG(error, __FILE__, __LINE__, log_msg);
		retVal = -1;
	}

	return retVal;	
}


int ImaService::getJsonStringValue(const std::string& jsonString, const char* pKey, string& value) {
	LOG(debug, __FILE__, __LINE__, "ImaService::getJsonStringValue called");

	int retVal= -1;
	json_t* pRoot = NULL;
	json_error_t error;
	pRoot = json_loads(jsonString.c_str(), 0, &error);
	if(pRoot) {
		json_t* pJsonObj = json_object_get(pRoot, pKey);
		if((pJsonObj != NULL) && (json_is_string(pJsonObj))) {
			retVal = 0;
			value = json_string_value(pJsonObj);
		}
	}
	
	json_decref(pRoot);
	return retVal;	
}

int ImaService::getJsonStringValue(const json_t* pRoot, const char* pKey, std::string& value) {
	LOG(debug, __FILE__, __LINE__, "ImaService::getJsonStringValue called");
	if(pRoot == NULL) {
		LOG(error, __FILE__, __LINE__, "JSON object is NULL");
		return -1;
	}

	if(pKey  == NULL) {
		LOG(error, __FILE__, __LINE__, "JSON Key to be searched is NULL");
		return -1;
	}

	int retVal = -1;
	json_t* pJsonObj = json_object_get(pRoot, pKey);
	if((pJsonObj != NULL) && (json_is_string(pJsonObj))) {
		retVal = 0;
		value = json_string_value(pJsonObj);
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Proper string value for %s not found in the JSON object.", pKey);
		LOG(error, __FILE__, __LINE__, log_msg);
		retVal = -1;
	}

	return retVal;	
}

int ImaService::extractMessagesFromJson(string& jsonString, std::vector<string>& messages) {
	LOG(debug, __FILE__, __LINE__, "ImaService::getJsonStringValue called");

	int retVal= -1;
	json_t* pRoot = NULL;
	json_error_t error;
	pRoot = json_loads(jsonString.c_str(), 0, &error);
	if(pRoot) {
		json_t* pJsonObj = json_object_get(pRoot, IMA_JSON_KEY_STATUS_MESSAGES);
		if((pJsonObj != NULL) && (json_is_array(pJsonObj))) {
			retVal = 0;
			int arraySize = json_array_size(pJsonObj);
			for(int i = 0; i < arraySize; i++) {
				json_t* pMsgObj = json_array_get(pJsonObj, i);
				messages.push_back(string(json_string_value(pMsgObj)));
			}
		}
	}
	
	json_decref(pRoot);
	return retVal;	
}


int ImaService::extractMessagesFromJsonAndAppendIntoXmlEle(string& jsonString, OMElement*& pMessagesEle) {
	LOG(debug, __FILE__, __LINE__, "ImaService::extractMessagesFromJsonAndAppendIntoXmlEle called");

	int retVal= -1;
	json_t* pRoot = NULL;
	json_error_t error;
	pRoot = json_loads(jsonString.c_str(), 0, &error);
	if(pRoot) {
		json_t* pJsonObj = json_object_get(pRoot, IMA_JSON_KEY_STATUS_MESSAGES);
		if((pJsonObj != NULL) && (json_is_array(pJsonObj))) {
			retVal = 0;
			int arraySize = json_array_size(pJsonObj);
			for(int i = 0; i < arraySize; i++) {
				json_t* pMsgObj = json_array_get(pJsonObj, i);
				createChildElement(pMessagesEle, IMA_SERVICE_XML_TAG_MESSAGE, json_string_value(pMsgObj)); 
			}
		}
	}
	
	json_decref(pRoot);
	return retVal;	
}

OMElement* ImaService::userFeedback(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::userFeedback called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_USERFEEDBACKRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	pRespElement->addChild(pMessagesElement);	

	string strSource, strSenderName, strSenderEmailId, strFeedbackSub, strFeedbackMsg;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_SOURCE, NULL));
		}
		
		strSenderName = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_NAME, NULL));
		strSenderEmailId = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_EMAILID, NULL));
		strFeedbackSub = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_SUBJECT, NULL));
		strFeedbackMsg = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_MESSAGE, NULL));
	
		if(strSenderEmailId.length() < 1) {
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Sender's Email Addrss not existing");
		}
	
		if(strFeedbackMsg.length() < 1) {
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Feedback Message not existing");
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "Empty message received as user feedback"); 
		status = -1;
		createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "empty message received aas user feedback");
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleUserFeedbackPkt(strSource, strSenderName, strSenderEmailId, strFeedbackSub, strFeedbackMsg);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		std::string respPkt;
		printf("json request pkt is %s\n", pJsonReqPkt);
		LOG(debug, __FILE__, __LINE__, "json request pkt is:");
		LOG(debug, __FILE__, __LINE__, pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			LOG(debug, __FILE__, __LINE__, "Returned from makeRequest with a success");
			long result;
			std::string tokenId;
			if(0 != getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) { 
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to send user feedback because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}


OMElement* ImaService::registerUser(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::registerUser called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_REGISTERRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	string strSource, strEmailId, strPassword, strName, strGender, strAddr, strCity, strState, strCountry, strZip, strPhone, strDob, strImId, strImService;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_SOURCE, NULL));
		}

		OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
		if(!pDetailsEle) {
			LOG(debug, __FILE__, __LINE__, "Details element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User details missing");
			printf("returned from createChildElement");
		} else {
			LOG(debug, __FILE__, __LINE__, "Details element found");
			strEmailId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_EMAILID, NULL));
			strPassword = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_PASSWORD, NULL));
			strName = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_NAME, NULL));
			strGender = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_GENDER, NULL));
			strAddr = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_ADDRESS, NULL));
			strCity = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_CITY, NULL));
			strState = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_STATE, NULL));
			strCountry = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_COUNTRY, NULL));
			strZip = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_ZIP, NULL));
			strPhone = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_PHONE, NULL));
			strDob = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_DOB, NULL));
			strImId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_IMID, NULL));
			strImService = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_IMSERVICE, NULL));
	
			if(strEmailId.length() < 1) {
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User email id is invalid");
			}
	
			if(strPassword.length() < 1) {
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User password is invalid");
			}
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "Empty message received for user registration"); 
		status = -1;
		createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "empty message");
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleUserRegistrationPkt(strSource, strEmailId, strPassword, strName, strGender, strAddr, strCity, strState, strCountry, strZip, strPhone, strDob, strImId, strImService);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			long result;
			std::string id;
			if((0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) 
				&& (0 == getJsonStringValue(respPkt, IMA_JSON_KEY_ID, id))) {
				if(result == 0) {
					createChildElement(pRespElement, IMA_SERVICE_XML_TAG_USERID, id.c_str());
				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make registration request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}

		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);
	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	bRetVal = pRespElement->addAttribute(pAttrib); //TODO: uncomment this
	if(bRetVal) {
		printf("AddAttribute was success\n");
	} else {
		printf("AddAttribute was failure\n");
	}

	return pRespElement;
}

OMElement* ImaService::confirmUserRegistration(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::confirmUserRegistration called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_CONFIRMUSERREGISTRATIONRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	string strSource, strEmailId;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_SOURCE, NULL));
		}

		OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
		if(!pDetailsEle) {
			LOG(debug, __FILE__, __LINE__, "Details element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User details missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Details element found");
			strEmailId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_EMAILID, NULL));
	
			if(strEmailId.length() < 1) {
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Either user-ID or User-email-id should be provided");
			}
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "Empty message received for logout"); 
		status = -1;
		createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "empty message");
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleRegistrationConfirmationPkt(strSource, strEmailId);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			long result;
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result != 0) {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make registration request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	
	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	bRetVal = pRespElement->addAttribute(pAttrib);

	return pRespElement;
}


OMElement* ImaService::updateUserDetails(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::updateUserDetails called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_UPDATEUSERDETAILSRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	std::string strSource, strEmailId, strTokenId;
	std::string strImId, strName, strAddr, strCity, strState, strCountry, strZip, strPhone, strGender, strDob;
	
	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "User %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "User %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		if(status == 0) {
			OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
			if(!pDetailsEle) {
				LOG(debug, __FILE__, __LINE__, "Details element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User's details to be updated missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Details element found");
				strImId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_IMID, NULL));
				strName = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_NAME, NULL));
				strAddr = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_ADDRESS, NULL));
				strCity = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_CITY, NULL));
				strState = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_STATE, NULL));
				strCountry = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_COUNTRY, NULL));
				strZip = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_ZIP, NULL));
				strPhone = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_PHONE, NULL));
				strGender = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_GENDER, NULL));
				strDob = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_DOB, NULL));
			}
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleUpdateUserDetailsPkt(strSource, strEmailId, strImId, strName, strAddr, strCity, strState, strCountry, strZip, strPhone, strGender, strDob);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result != 0) {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for update user details received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make updateUserDetails request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}


int ImaService::fetchUserData(std::string& strSource, std::string& strEmailId, OMElement*& pRespElement) {
	LOG(debug, __FILE__, __LINE__, "ImaService::fetchUserData called during login");

	int status = 0;
	pRespElement = new OMElement(IMA_SERVICE_XML_TAG_USERDATA);

	long result = 0;
	LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
	ImaJsonAssembler jsonAssembler;
	char* pJsonReqPkt = jsonAssembler.assembleFetchUserDataPkt(strSource, strEmailId);
	ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
	std::string respPkt;
	printf("json request pkt is %s\n", pJsonReqPkt);
	if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
		LOG(debug, __FILE__, __LINE__, "json response is:");
		LOG(debug, __FILE__, __LINE__, respPkt.c_str());
		if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
			if(result == 0) {
				//Extract the JSON messages and assemble XML
				std::string errMsgIfAny;
				if(false == assembleFetchUserDataXmlResponse(respPkt, pRespElement, errMsgIfAny)) {
					LOG(error, __FILE__, __LINE__, errMsgIfAny.c_str());
					status = -1;
				}
			} else {
				LOG(error, __FILE__, __LINE__, "App server returned non-zero while fetching user data during login");
				status = -1;
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "The JSON response for fetch-user-data received from the appserver is not correct");
			LOG(error, __FILE__, __LINE__, log_msg);
		}
	} else {
		status = -1;
		char log_msg[1024];
		sprintf(log_msg, "Failed to make fetch-user-data request because of some communication error");
		LOG(error, __FILE__, __LINE__, log_msg);
	}
	free(pJsonReqPkt);

	return status;
}


OMElement* ImaService::login(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::login called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_LOGINRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	pRespElement->addChild(pMessagesElement);	

	string strSource, strEmailId, strPassword, strConfCode;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_SOURCE, NULL));
		}

		OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
		if(!pDetailsEle) {
			LOG(debug, __FILE__, __LINE__, "Details element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User details missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Details element found");
			strEmailId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_EMAILID, NULL));
			strPassword = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_PASSWORD, NULL));
			strConfCode = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_CONFCODE, NULL));
	
			if(strEmailId.length() < 1) {
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Either user-ID or User-email-id should be provided");
			}
	
			if(strPassword.length() < 1) {
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User password is invalid");
			}
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "Empty message received for user login"); 
		status = -1;
		createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "empty message");
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleLoginPkt(strSource, strEmailId, strPassword, strConfCode);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		std::string respPkt;
		printf("json request pkt is %s\n", pJsonReqPkt);
		LOG(debug, __FILE__, __LINE__, "json request pkt is:");
		LOG(debug, __FILE__, __LINE__, pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			LOG(debug, __FILE__, __LINE__, "Returned from makeRequest with a success");
			long result;
			std::string tokenId;
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) { 
				if(result == 0) {
					OMElement* pFetchUserDataRespEle = NULL;
					status = fetchUserData(strSource, strEmailId, pFetchUserDataRespEle);
					if(status == 0) {
						createChildElement(pRespElement, IMA_SERVICE_XML_TAG_EMAILID, strEmailId.c_str());
						uint32_t ipAddress = 123;	//TODO: use the real ip address here
						std::string strTokenId = ImaServiceUtils::generateTokenId(ipAddress);
						ImaActiveUsers::getInstance()->insertUser(strEmailId, strTokenId);
						createChildElement(pRespElement, IMA_SERVICE_XML_TAG_TOKENID, strTokenId.c_str());
						pRespElement->addChild(pFetchUserDataRespEle);
					} else {
						createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Failed to do fetch-user-data while doing processing login request");
						LOG(error, __FILE__, __LINE__, "Failed to do fetch-user-data while doing processing login request");
						status = -1;
					}
				} else if(result == IMA_RESPONSE_REGISTRATION_CONFIRMATION_PENDING) {
					LOG(debug, __FILE__, __LINE__, "The registration status of the user is still pending");
					createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "The registration status of the user is still pending");
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
					status = IMA_RESPONSE_REGISTRATION_CONFIRMATION_PENDING;
				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make login request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

OMElement* ImaService::changePassword(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::changePassword called");
	
	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_CHANGEPASSWORDRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);

	std::string strSource, strEmailId, strTokenId, strCurrPassword, strNewPassword;
	
	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source element missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		OMElement* pPasswordEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_PASSWORD), NULL);
		if(!pPasswordEle) {
			LOG(debug, __FILE__, __LINE__, " Current Password element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Current password missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Current password element found");
			strCurrPassword = getElementText(pPasswordEle);
		}

		OMElement* pNewPasswordEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_NEWPASSWORD), NULL);
		if(!pNewPasswordEle) {
			LOG(debug, __FILE__, __LINE__, "New Password element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "New password missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "New password element found");
			strNewPassword = getElementText(pNewPasswordEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "USer %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "USer %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleChangePasswordPkt(strSource, strEmailId, strCurrPassword, strNewPassword);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result != 0) {
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
					status = -1;
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for changepassword, received from the appserver is not correct; Something went wrong!!!");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make changepassword request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}
	
	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

OMElement* ImaService::forgotPassword(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::forgotPassword called");
	
	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_FORGOTPASSWORDRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);

	std::string strSource, strEmailId;
	
	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source element missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleForgotPasswordPkt(strSource, strEmailId);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result != 0) {
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
					status = -1;
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for forgotpassword, received from the appserver is not correct; Something went wrong!!!");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make forgotpassword request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}
	
	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

OMElement* ImaService::logout(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::logout called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_LOGOUTRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	pRespElement->addChild(pMessagesElement);	

	string strSource, strEmailId, strTokenId;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_SOURCE, NULL));
		}

		OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
		if(!pDetailsEle) {
			LOG(debug, __FILE__, __LINE__, "Details element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User details missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Details element found");
			strEmailId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_EMAILID, NULL));
			strTokenId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_TOKENID, NULL));
	
			if(strTokenId.length() < 1) {
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "User-email-id should be provided");
			}
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "Empty message received for logout"); 
		status = -1;
		createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "empty message");
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		if(!ImaActiveUsers::getInstance()->removeUser(strEmailId, strTokenId)) {
			status = -1;
			LOG(info, __FILE__, __LINE__, "Failed to logout; UserId or TokenId mismatch");
		} else {
			createChildElement(pRespElement, IMA_SERVICE_XML_TAG_EMAILID, strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, "User has been logged out");
		}
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	
	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

OMElement* ImaService::fetchUserData(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::fetchUserData called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_FETCHUSERDATARESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	string strSource, strEmailId, strTokenId;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(inMsg->getChildElement(IMA_SERVICE_XML_TAG_SOURCE, NULL));
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}

		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "USer %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "USer %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

	} else {
		LOG(debug, __FILE__, __LINE__, "Empty message received for fetching user data"); 
		status = -1;
		createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Empty message received for fetching user data");
	}

	long result = 0;
	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleFetchUserDataPkt(strSource, strEmailId);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		std::string respPkt;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			LOG(debug, __FILE__, __LINE__, "json response is:");
			LOG(debug, __FILE__, __LINE__, respPkt.c_str());
			printf("%s", respPkt.c_str());
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result == 0) {
					//Extract the JSON messages and assemble XML
					std::string errMsgIfAny;
					if(false == assembleFetchUserDataXmlResponse(respPkt, pRespElement, errMsgIfAny)) {
						LOG(error, __FILE__, __LINE__, errMsgIfAny.c_str());
						createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, errMsgIfAny.c_str());
						status = -1;
					}
				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for addInfo, received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make fetch-user-data request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;

}

OMElement* ImaService::addInfo(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::addInfo called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_ADDINFORESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	string strSource, strEmailId, strTokenId, strInfo, strExpiryDate;
	int media;
	unsigned int ttl;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "USer %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "USer %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		if(status == 0) {
			OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
			if(!pDetailsEle) {
				LOG(debug, __FILE__, __LINE__, "Details element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Info details missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Details element found");
				strInfo = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_INFO, NULL));
				std::string temp = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_MEDIA, NULL));
				media = atoi(temp.c_str());
				strExpiryDate = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_EXPIRY_DATE, NULL));
				temp = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_TTL, NULL));
				ttl = atol(temp.c_str());
			}
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleAddInfoPkt(strSource, strEmailId, strInfo, media, strExpiryDate, ttl);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result == 0) {
					std::string strInfoId;
					getJsonStringValue(respPkt, IMA_JSON_KEY_ID, strInfoId);
					//OMElement* pDetailsElement = new OMElement(IMA_SERVICE_XML_TAG_DETAILS);
					//bRetVal = pRespElement->addChild(pDetailsElement);	
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_INFOID, strInfoId.c_str());
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_INFO, strInfo.c_str());
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_MEDIA, media);
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_EXPIRY_DATE, strExpiryDate.c_str());
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_TTL, ttl);
					createChildElement(pRespElement, IMA_SERVICE_XML_TAG_INFOID, strInfoId.c_str());
				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for addInfo, received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make addInfo request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

OMElement* ImaService::updateInfo(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::updateInfo called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_UPDATEINFORESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	string strSource, strEmailId, strTokenId, strInfo, strExpiryDate;
	long infoId;
	int media;
	unsigned int ttl;
	std::vector<unsigned long> recipientsToBeDisassociated;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "USer %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "USer %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		if(status == 0) {
			OMElement* pInfoIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_INFOID), NULL);
			if(!pInfoIdEle) {
				LOG(debug, __FILE__, __LINE__, "Info Id element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Info Id missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Info ID element found");
				std::string strInfoId = getElementText(pInfoIdEle);
				infoId = atol(strInfoId.c_str());
				if(infoId <= 0) {
					LOG(debug, __FILE__, __LINE__, "Info id cannot be <= 0");
					status = -1;
					createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Info id cannot be <= 0");
				}
			}
		}

		if(status == 0) {
			OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
			if(!pDetailsEle) {
				LOG(debug, __FILE__, __LINE__, "Details element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Info details missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Details element found");
				strInfo = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_INFO, NULL));
				std::string temp = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_MEDIA, NULL));
				media = atoi(temp.c_str());
				strExpiryDate = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_EXPIRY_DATE, NULL));
				temp = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_TTL, NULL));
				ttl = atol(temp.c_str());
				//get recipients to be disasscociated
				std::vector<unsigned long> recipientsToBeDeleted;
				OMElement* pRecipientsToBeDeletedEle = pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_RECIPIENTSTOBEDISASSOCIATED, NULL);
				if(pRecipientsToBeDeletedEle != NULL) {
					extractRecipientIds(pRecipientsToBeDeletedEle, recipientsToBeDisassociated);
				}
			}
		}
	} else {
		LOG(error, __FILE__, __LINE__, "inMsg is NULL");
		status = -1;
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleUpdateInfoPkt(strSource, strEmailId, infoId, strInfo, media, strExpiryDate, ttl, recipientsToBeDisassociated);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result == 0) {
					std::string strInfoId;
					getJsonStringValue(respPkt, IMA_JSON_KEY_ID, strInfoId);
					createChildElement(pRespElement, IMA_SERVICE_XML_TAG_INFOID, strInfoId.c_str());
				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for updateInfo received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make updateInfo request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}


OMElement* ImaService::deleteInfo(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::deleteInfo called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_DELETEINFORESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	pRespElement->addChild(pMessagesElement);	

	string strSource, strEmailId, strTokenId;
	long infoId;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		OMElement* pInfoIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_INFOID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Info Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Info Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Info ID element found");
			std::string strInfoId = getElementText(pInfoIdEle);
			infoId = atol(strInfoId.c_str());
			if(infoId <= 0) {
				LOG(debug, __FILE__, __LINE__, "Info ID can't be <=0");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Info ID can't be <=0");
			}
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "User %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "User %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleDeleteInfoPkt(strSource, strEmailId, infoId);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result == 0) {
					std::string strInfoId;
					getJsonStringValue(respPkt, IMA_JSON_KEY_ID, strInfoId);
					createChildElement(pRespElement, IMA_SERVICE_XML_TAG_INFOID, strInfoId.c_str());
				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for deleteInfo received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make deleteInfo request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

bool ImaService::getRecipientDetails(OMElement* pRecipientEle, INFO_RECIPIENT*& pRecipient) {
	LOG(debug, __FILE__, __LINE__, "ImaService::getRecipientDetails called");

	bool bRetVal = false;

	pRecipient = new INFO_RECIPIENT;
	pRecipient->recipientId = 0;
	
	OMElement* pEle = pRecipientEle->getChildElement(IMA_SERVICE_XML_TAG_ID, NULL);
	if(pEle) {
		long recipientId = atol(getElementText(pEle).c_str());
		if(recipientId > 0) {
			pRecipient->recipientId = recipientId;
			bRetVal = true;	
		}
	} else {
		OMElement* pEle = pRecipientEle->getChildElement(IMA_SERVICE_XML_TAG_NAME, NULL);
		if(pEle) {
			pRecipient->strName = getElementText(pEle);
		}

		pEle = pRecipientEle->getChildElement(IMA_SERVICE_XML_TAG_EMAILID, NULL);
		if(pEle) {
			pRecipient->strEmailId= getElementText(pEle);
		}
		
		pEle = pRecipientEle->getChildElement(IMA_SERVICE_XML_TAG_PHONE, NULL);
		if(pEle) {
			pRecipient->strPhone= getElementText(pEle);
		}

		pEle = pRecipientEle->getChildElement(IMA_SERVICE_XML_TAG_ADDRESS, NULL);
		if(pEle) {
			pRecipient->strAddress= getElementText(pEle);
		}

		pEle = pRecipientEle->getChildElement(IMA_SERVICE_XML_TAG_RELATION_WITH_OWNER, NULL);
		if(pEle) {
			pRecipient->strRelationship = getElementText(pEle);
		}
	}

	if((pRecipient->recipientId > 0)
		|| pRecipient->strName.length() 
		|| pRecipient->strEmailId.length()
		|| pRecipient->strPhone.length()
		|| pRecipient->strRelationship.length()) {
		LOG(debug, __FILE__, __LINE__, "Required information for the recipient is available");
		bRetVal = true;	
	} else {
		LOG(debug, __FILE__, __LINE__, "No relevant information found for the recipient");
		delete pRecipient;
		pRecipient = NULL;
	}

	return bRetVal;
}

/*
Returns the no. of recipients found and the recipients information 
is stored in the output parameter
*/		
int ImaService::extractInfoRecipients(OMElement* pRecipientsEle, 
				std::vector<INFO_RECIPIENT*>& recipientVector) {
	LOG(debug, __FILE__, __LINE__, "ImaService::extractInfoRecipients called");

	int retVal = 0;	
	std::vector<OMNode*> recipientNodes = pRecipientsEle->getChildren();

	std::vector<OMNode*>::iterator it;
	for(it = recipientNodes.begin(); it != recipientNodes.end(); it++) {
		OMElement* pEle = dynamic_cast<OMElement*>(*it);
		if(pEle && (strcmp(pEle->getLocalname().c_str(), IMA_SERVICE_XML_TAG_RECIPIENT) == 0)) {
			INFO_RECIPIENT* pInfoRecipient = NULL;
			if(getRecipientDetails(pEle, pInfoRecipient)) {
				recipientVector.push_back(pInfoRecipient);
				retVal++;
			}
		}
	}

	char log_msg[1024];
	sprintf(log_msg, "No. of recipients is %d", retVal);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return retVal;
}

int ImaService::extractRecipientIds(OMElement* pElement, std::vector<unsigned long>& recipientIds) {
	LOG(debug, __FILE__, __LINE__, "ImaService::extractRecipientIds called");

	int retVal = 0;	
	std::vector<OMNode*> recipientNodes = pElement->getChildren();

	std::vector<OMNode*>::iterator it;
	for(it = recipientNodes.begin(); it != recipientNodes.end(); it++) {
		OMElement* pRecipientEle = dynamic_cast<OMElement*>(*it);
		if(pRecipientEle && (strcmp(pRecipientEle->getLocalname().c_str(), IMA_SERVICE_XML_TAG_RECIPIENT) == 0)) {
			OMElement* pIdEle = pRecipientEle->getFirstElement();
			if(pIdEle && (strcmp(pIdEle->getLocalname().c_str(), IMA_SERVICE_XML_TAG_ID) == 0)) {
				std::string strId = getElementText(pIdEle);
				long id = atol(strId.c_str());
				if(id > 0) {
					recipientIds.push_back(id);
					retVal++;
				} else {
					LOG(info, __FILE__, __LINE__, "corrupt packet received");
					recipientIds.clear();
					return 0;
				}
			}
		}
	}

	char log_msg[1024];
	sprintf(log_msg, "No. of recipients is %d", retVal);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return retVal;
}





bool ImaService::assembleFetchUserDataXmlResponse(
			const std::string& strJsonStr,
			OMElement* pRespElement, std::string& errMsgIfAny) {
	LOG(debug, __FILE__, __LINE__, "ImaService::assembleFetchUserDataXmlResponse");
	bool bRetVal = true;

	json_t* pRoot = NULL;
	json_error_t error;
	pRoot = json_loads(strJsonStr.c_str(), 0, &error);
	if(!pRoot) {
		errMsgIfAny = std::string("Failed to parse User Data response pkt received from appserver.");
		return false;
	}

	json_t* pUserDetailsJsonObj = json_object_get(pRoot, IMA_JSON_KEY_USER_DETAILS);
	if(!pUserDetailsJsonObj) {
		errMsgIfAny = std::string("User Deatils not found in the Json response pkt received from appserver.");
		bRetVal = false;
	}

	if(false == bRetVal) {
		json_decref(pRoot);
		return bRetVal;
	}

	json_t* pRecipientListObj = json_object_get(pRoot, IMA_JSON_KEY_RECIPIENT_LIST);
	if((!pRecipientListObj) || (false == json_is_array(pRecipientListObj))){
		errMsgIfAny = std::string("List of recipients not found in the Json response pkt received from appserver.");
		bRetVal = false;
	}

	if(false == bRetVal) {
		json_decref(pRoot);
		return bRetVal;
	}

	json_t* pInfoListObj = json_object_get(pRoot, IMA_JSON_KEY_INFO_LIST);
	if((!pInfoListObj) || (false == json_is_array(pInfoListObj))){
		errMsgIfAny = std::string("List of info not found in the Json response pkt received from appserver.");
		bRetVal = false;
	}

	if(false == bRetVal) {
		json_decref(pRoot);
		return bRetVal;
	}

	OMElement* pUserDetailsEle = new OMElement(IMA_SERVICE_XML_TAG_USERDETAILS);
	assembleUserDetailsObject(pUserDetailsJsonObj, pUserDetailsEle);
	pRespElement->addChild(pUserDetailsEle);

	OMElement* pRecipientListEle = new OMElement(IMA_SERVICE_XML_TAG_RECIPIENTLIST);
	assembleRecipientListObject(pRecipientListObj, pRecipientListEle);
	pRespElement->addChild(pRecipientListEle);

	OMElement* pInfoListEle = new OMElement(IMA_SERVICE_XML_TAG_INFOLIST);
	assembleInfoListObject(pInfoListObj, pInfoListEle);
	pRespElement->addChild(pInfoListEle);

	json_decref(pRoot);

	return bRetVal;	
}

bool ImaService::assembleUserDetailsObject(const json_t* pUserDetailsJsonObj, OMElement* pUserDetailsEle) {
	LOG(debug, __FILE__, __LINE__, "ImaService::assembleUserDetailsObject");
	std::string strEmailId, strName, strAddress, strCity, strState, strCountry, strZip, strPhone, strDob, strImId, strImService, strGender;

	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_EMAIL, strEmailId);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_NAME, strName);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_ADDRESS, strAddress);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_CITY, strCity);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_STATE, strState);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_COUNTRY, strCountry);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_ZIP, strZip);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_PHONE, strPhone);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_DOB, strDob);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_IM_ID, strImId);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_IM_SERVICE, strImService);
	getJsonStringValue(pUserDetailsJsonObj, IMA_JSON_KEY_GENDER, strGender);

	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_EMAILID, strEmailId.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_NAME, strName.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_ADDRESS, strAddress.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_CITY, strCity.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_STATE, strState.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_COUNTRY, strCountry.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_ZIP, strZip.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_PHONE, strPhone.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_DOB, strDob.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_IMID, strImId.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_IMSERVICE, strImService.c_str());
	createChildElement(pUserDetailsEle, IMA_SERVICE_XML_TAG_GENDER, strGender.c_str());

	return true;
}

bool ImaService::assembleRecipientListObject(const json_t* pRecipientListJsonObj, OMElement* pRecipientListEle) {
	LOG(debug, __FILE__, __LINE__, "ImaService::assembleRecipientListObject");

	size_t numRecipients = json_array_size(pRecipientListJsonObj);

	//loop into the recipient array
	for(unsigned int i = 0; i < numRecipients; i++) {
		unsigned long recipientId = 0;
		const char* pRecipientName = "";
		const char* pRecipientEmail = "";
		const char* pRecipientPhone = "";
		const char* pRecipientAddress = "";
		const char* pRecipientRelationWithOwner = "";

		json_t* pRecipientObject = json_array_get(pRecipientListJsonObj, i);
		if(!pRecipientObject) {
			char log_msg[1024];
			sprintf(log_msg, "Not a proper recipient object at index %u", i);
			LOG(error, __FILE__, __LINE__, log_msg);
			continue;
		}

		if(extractRecipientDetails(pRecipientObject, recipientId, pRecipientName, pRecipientEmail, pRecipientPhone, pRecipientAddress, pRecipientRelationWithOwner) == 0) {
			OMElement* pRecipientEle = new OMElement(IMA_SERVICE_XML_TAG_RECIPIENT);
			char tempRecipientId[256];
			sprintf(tempRecipientId, "%lu", recipientId);
			createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_ID, tempRecipientId);
			createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_NAME, pRecipientName);
			createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_EMAILID, pRecipientEmail);
			createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_PHONE, pRecipientPhone);
			createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_ADDRESS, pRecipientAddress);
			createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_RELATION_WITH_OWNER, pRecipientRelationWithOwner);

			pRecipientListEle->addChild(pRecipientEle);

		} else {
			LOG(error, __FILE__, __LINE__, "Failed to extract info related to a recipient");
		}
	}

	return true;	//Look into this later
}


bool ImaService::assembleInfoListObject(const json_t* pInfoListJsonObj, OMElement* pInfoListEle) {
	LOG(debug, __FILE__, __LINE__, "ImaService::assembleInfoListObject");

	size_t numInfo = json_array_size(pInfoListJsonObj);

	//loop into the recipient array
	for(unsigned int i = 0; i < numInfo; i++) {
		unsigned long infoId = 0;
		const char* pInfoText = "";
		unsigned short media = 0;
		unsigned long ttl = 0;
		std::vector<unsigned long> infoRecipientsList;

		json_t* pInfoObject = json_array_get(pInfoListJsonObj, i);
		if(!pInfoObject) {
			char log_msg[512];
			sprintf(log_msg, "Not a proper info object at index %u", i);
			LOG(error, __FILE__, __LINE__, log_msg);
			continue;
		}

		OMElement* pInfoEle = new OMElement(IMA_SERVICE_XML_TAG_INFO);
		//MOVE FROM HERE: 10th Aug 2013: American center Library
		if(extractInfoDetails(pInfoObject, infoId, pInfoText, media, ttl, infoRecipientsList) == 0) {
			char tempInfoId[256]; sprintf(tempInfoId, "%lu", infoId);
			createChildElement(pInfoEle, IMA_SERVICE_XML_TAG_ID, tempInfoId);
			createChildElement(pInfoEle, IMA_SERVICE_XML_TAG_TEXT, pInfoText);
			char tempMedia[64]; sprintf(tempMedia, "%u", media);
			createChildElement(pInfoEle, IMA_SERVICE_XML_TAG_MEDIA, tempMedia);
			char tempTtl[64]; sprintf(tempTtl, "%lu", ttl);
			createChildElement(pInfoEle, IMA_SERVICE_XML_TAG_TTL, tempTtl);

			OMElement* pRecipientsEle = new OMElement(IMA_SERVICE_XML_TAG_RECIPIENTS);
			std::vector<unsigned long>::iterator it;
			for(it = infoRecipientsList.begin(); it != infoRecipientsList.end(); it++) {
				char tempId[128]; sprintf(tempId, "%lu", *it);
				createChildElement(pRecipientsEle, IMA_SERVICE_XML_TAG_ID, tempId);
			}

			pInfoEle->addChild(pRecipientsEle);

		} else {
			LOG(error, __FILE__, __LINE__, "Failed to extract info related to a recipient");
		}

		pInfoListEle->addChild(pInfoEle);
	}

	return true;
}


int ImaService::extractRecipientDetails(const json_t* pRecipientObject, unsigned long& recipientId, const char*& pRecipientName, const char*& pRecipientEmail, const char*& pRecipientPhone, const char*& pRecipientAddress, const char*& pRecipientRelationWithOwner) {
	LOG(debug, __FILE__, __LINE__, "ImaService::extractRecipientDetails");

	int retVal = 0;
	char log_msg[1024];

	recipientId = 0;
	json_t* pRecipientIdObject = json_object_get(pRecipientObject, IMA_JSON_KEY_ID);
	if(pRecipientIdObject != NULL) {
		if(json_is_integer(pRecipientIdObject) == true) {
			long temp = json_integer_value(pRecipientIdObject);
			if(temp > 0) {
				recipientId = temp;
			} else {
				sprintf(log_msg, "Not a valid recipient id");
				LOG(error, __FILE__, __LINE__, log_msg);
				retVal = -1;
			}
		} else {
			sprintf(log_msg, "Not a valid recipient id");
			LOG(debug, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}

	if((retVal == 0) && (recipientId > 0)) {
		LOG(debug, __FILE__, __LINE__, "legal recipient id found");
	} else if(retVal < 0) {
		LOG(error, __FILE__, __LINE__, "Illegal recipient ID");
	}


	json_t* pRecipientNameObject = json_object_get(pRecipientObject, IMA_JSON_KEY_NAME);
	if(pRecipientNameObject != NULL) {
		if(json_is_string(pRecipientNameObject) == true) {
			pRecipientName = json_string_value(pRecipientNameObject);
			if(strlen(pRecipientName) <= 0) {
				LOG(debug, __FILE__, __LINE__, "Recipient name is empty");
				retVal = -1;
			}
		} else {
			sprintf(log_msg, "Not a valid recipient name");
			LOG(debug, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "Recipient name is mandatory");
		retVal = -1;
	}

	json_t* pRecipientEmailObject = json_object_get(pRecipientObject, IMA_JSON_KEY_EMAIL);
	if(pRecipientEmailObject != NULL) {
		if(json_is_string(pRecipientEmailObject) == true) {
			pRecipientEmail = json_string_value(pRecipientEmailObject);
		} else {
			sprintf(log_msg, "Not a valid recipient recipient email id");
			LOG(debug, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}

	json_t* pRecipientPhoneObject = json_object_get(pRecipientObject, IMA_JSON_KEY_PHONE);
	if(pRecipientPhoneObject != NULL) {
		if(json_is_string(pRecipientPhoneObject) == true) {
			pRecipientPhone = json_string_value(pRecipientPhoneObject);
		} else {
			sprintf(log_msg, "Not a valid recipient phone num ");
			LOG(debug, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}
	
	json_t* pRecipientAddressObject = json_object_get(pRecipientObject, IMA_JSON_KEY_ADDRESS);
	if(pRecipientAddressObject != NULL) {
		if(json_is_string(pRecipientAddressObject) == true) {
			pRecipientAddress = json_string_value(pRecipientAddressObject);
		} else {
			sprintf(log_msg, "Not a valid recipient address");
			LOG(debug, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}

	json_t* pRecipientRelationWithOwnerObject = json_object_get(pRecipientObject, IMA_JSON_KEY_RELATION_WITH_OWNER);
	if(pRecipientRelationWithOwnerObject!= NULL) {
		if(json_is_string(pRecipientRelationWithOwnerObject) == true) {
			pRecipientRelationWithOwner = json_string_value(pRecipientRelationWithOwnerObject);
		} else {
			sprintf(log_msg, "Not a valid recipient relation with owner");
			LOG(debug, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}

	return retVal;	
}

int ImaService::extractInfoDetails(const json_t* pInfoObject, unsigned long& infoId, const char*& pInfoText, unsigned short& media, unsigned long& ttl, std::vector<unsigned long>& recipientIdList) {
	LOG(debug, __FILE__, __LINE__, "ImaService::extractInfoDetails");

	int retVal = 0;
	char log_msg[1024];

	infoId = 0;
	json_t* pInfoIdObject = json_object_get(pInfoObject, IMA_JSON_KEY_ID);
	if(pInfoIdObject != NULL) {
		if(json_is_integer(pInfoIdObject) == true) {
			long temp = json_integer_value(pInfoIdObject);
			if(temp > 0) {
				infoId = temp;
			} else {
				sprintf(log_msg, "Not a valid info id");
				LOG(error, __FILE__, __LINE__, log_msg);
				retVal = -1;
			}
		} else {
			sprintf(log_msg, "Not a valid info id");
			LOG(error, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}

	if((retVal == 0) && (infoId > 0)) {
		LOG(debug, __FILE__, __LINE__, "legal info id found");
	} else if(retVal < 0) {
		LOG(error, __FILE__, __LINE__, "Illegal info id");
	}


	json_t* pInoTextObject = json_object_get(pInfoObject, IMA_JSON_KEY_INFO);
	if(pInoTextObject != NULL) {
		if(json_is_string(pInoTextObject) == true) {
			pInfoText = json_string_value(pInoTextObject);
		} else {
			sprintf(log_msg, "Not a info text");
			LOG(debug, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}

	json_t* pInfoMediaObject = json_object_get(pInfoObject, IMA_JSON_KEY_MEDIA);
	if(pInfoMediaObject != NULL) {
		if(json_is_integer(pInfoMediaObject) == true) {
			short temp = json_integer_value(pInfoMediaObject);
			if(temp >= 0) {
				media = temp;
			} else {
				sprintf(log_msg, "Not a valid info media");
				LOG(error, __FILE__, __LINE__, log_msg);
				retVal = -1;
			}
		} else {
			sprintf(log_msg, "Not a valid info media");
			LOG(error, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}

	json_t* pInfoTtlObject = json_object_get(pInfoObject, IMA_JSON_KEY_TTL);
	if(pInfoTtlObject != NULL) {
		if(json_is_integer(pInfoTtlObject) == true) {
			long temp = json_integer_value(pInfoTtlObject);
			if(temp >= 0) {
				ttl = temp;
			} else {
				sprintf(log_msg, "Not a valid TTL");
				LOG(error, __FILE__, __LINE__, log_msg);
				retVal = -1;
			}
		} else {
			sprintf(log_msg, "Not a valid TTL");
			LOG(error, __FILE__, __LINE__, log_msg);
			retVal = -1;
		}
	}

	json_t* pInfoRecipientsObject = json_object_get(pInfoObject, IMA_JSON_KEY_RECIPIENTS);
	if((!pInfoRecipientsObject) || (false == json_is_array(pInfoRecipientsObject))){
		std::string errMsg("List of recipients not found for this info.");
		LOG(error, __FILE__, __LINE__, errMsg.c_str());
		retVal = -1;
	} else {
		extractInfoRecipients(pInfoRecipientsObject, recipientIdList);
	}

	return retVal;	
}

int ImaService::extractInfoRecipients(const json_t* pRecipientIdListJsonObj, std::vector<unsigned long>& recipientIdList) {
	LOG(debug, __FILE__, __LINE__, "ImaService::extractInfoRecipients");

	size_t numRecipients = json_array_size(pRecipientIdListJsonObj);

	//loop into the recipient array
	for(unsigned int i = 0; i < numRecipients; i++) {
		unsigned long recipientId = 0;

		json_t* pRecipientIdObject = json_array_get(pRecipientIdListJsonObj, i);
		if((!pRecipientIdObject) || (false == json_is_integer(pRecipientIdObject))) {
			char log_msg[1024];
			sprintf(log_msg, "Not a proper recipient id object at index %u", i);
			LOG(error, __FILE__, __LINE__, log_msg);
			continue;
		} else {
			recipientId = json_integer_value(pRecipientIdObject);
			recipientIdList.push_back(recipientId);
		}
	}

	return 0;
}

bool ImaService::assembleAddInfoRecipientsXmlResponse(
				const std::string& strJsonRespPkt, 
				std::vector<INFO_RECIPIENT*>& recipientVector, OMElement* pRespElement) {
	LOG(debug, __FILE__, __LINE__, "ImaService::assembleAddInfoRecipientsXmlResponse");

	bool bRetVal = true;
	std::string strRecipientIds;
	getJsonStringValue(strJsonRespPkt, IMA_JSON_KEY_ID, strRecipientIds);
	OMElement* pRecipientsElement = new OMElement(IMA_SERVICE_XML_TAG_RECIPIENTS);
	bRetVal = pRespElement->addChild(pRecipientsElement);

	std::size_t startPos = 0;	
	std::vector<INFO_RECIPIENT*>::iterator it = recipientVector.begin();
	std::size_t found = strRecipientIds.find_first_of(IMA_JSON_ID_DELIMITER, startPos);
	char recipientId[64];
	while(found != std::string::npos) {
		strRecipientIds.copy(recipientId, found - startPos, startPos);
		recipientId[found - startPos] = '\0';
		startPos = found + 1;
		found = strRecipientIds.find_first_of(IMA_JSON_ID_DELIMITER, startPos);
//		if(it == recipientVector.end()) {
//			LOG(error, __FILE__, __LINE__, "Something wrong so that the recipient info not found in recipient vector");
//			bRetVal = false;
//			break;
//		} else {
			//INFO_RECIPIENT* pInfoRec = *it;
			if(strlen(recipientId) > 0) {
				OMElement* pRecipientEle = createChildElement(pRecipientsElement, IMA_SERVICE_XML_TAG_RECIPIENT);
				createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_ID, recipientId);
			}
			//addAttribute(IMA_SERVICE_XML_ATTRIB_RECIPIENTID, recipientId, pRecipientEle);
			//if(pInfoRec->recipientId > 0) {
			//	char strId[256];
			//	sprintf(strId, "%lu", pInfoRec->recipientId);
			//	//addAttribute(IMA_SERVICE_XML_ATTRIB_ID, strId, pRecipientEle);
			//	createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_ID, strId);
			//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_NAME, pInfoRec->strName);
			//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_EMAILID, pInfoRec->strEmailId);
			//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_PHONE, pInfoRec->strPhone);
			//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_ADDRESS, pInfoRec->strAddress);
			//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_RELATIONSHIP_WITH_OWNER, pInfoRec->strRelationship);
			//}
			//addAttribute(IMA_SERVICE_XML_ATTRIB_NAME, pInfoRec->strName, pRecipientEle);
			//addAttribute(IMA_SERVICE_XML_ATTRIB_EMAILID, pInfoRec->strEmailId, pRecipientEle);
			//addAttribute(IMA_SERVICE_XML_ATTRIB_PHONE, pInfoRec->strPhone, pRecipientEle);
			//addAttribute(IMA_SERVICE_XML_ATTRIB_ADDRESS, pInfoRec->strAddress, pRecipientEle);
			//addAttribute(IMA_SERVICE_XML_ATTRIB_RELATIONSHIP_WITH_OWNER, pInfoRec->strRelationship, pRecipientEle);
//		}
//		it++;
	}

	strRecipientIds.copy(recipientId, strRecipientIds.length() - startPos, startPos);
	recipientId[strRecipientIds.length() - startPos] = '\0'; 
	//if(it == recipientVector.end()) {
	//	LOG(error, __FILE__, __LINE__, "Something wrong so that the recipient info not found in recipient vector");
	//	bRetVal = false;
	//} else {
		//INFO_RECIPIENT* pInfoRec = *it;
		if(strlen(recipientId) > 0) {
			OMElement* pRecipientEle = createChildElement(pRecipientsElement, IMA_SERVICE_XML_TAG_RECIPIENT);			
			createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_ID, recipientId);
		}
		//addAttribute(IMA_SERVICE_XML_ATTRIB_RECIPIENTID, recipientId, pRecipientEle);
		//if(pInfoRec->recipientId > 0) {
		//	char strId[256];
		//	sprintf(strId, "%lu", pInfoRec->recipientId);
		//	//addAttribute(IMA_SERVICE_XML_ATTRIB_ID, strId, pRecipientEle);
		//	createChildElement(pRecipientEle, IMA_SERVICE_XML_TAG_ID, strId);
		//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_NAME, pInfoRec->strName);
		//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_EMAILID, pInfoRec->strEmailId);
		//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_PHONE, pInfoRec->strPhone);
		//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_ADDRESS, pInfoRec->strAddress);
		//	createChildElement(pRecipientEle,  IMA_SERVICE_XML_TAG_RELATIONSHIP_WITH_OWNER, pInfoRec->strRelationship);
		//}
		//addAttribute(IMA_SERVICE_XML_ATTRIB_NAME, pInfoRec->strName, pRecipientEle);
		//addAttribute(IMA_SERVICE_XML_ATTRIB_EMAILID, pInfoRec->strEmailId, pRecipientEle);
		//addAttribute(IMA_SERVICE_XML_ATTRIB_PHONE, pInfoRec->strPhone, pRecipientEle);
		//addAttribute(IMA_SERVICE_XML_ATTRIB_ADDRESS, pInfoRec->strAddress, pRecipientEle);
		//addAttribute(IMA_SERVICE_XML_ATTRIB_RELATIONSHIP_WITH_OWNER, pInfoRec->strRelationship, pRecipientEle);
	//}

	return bRetVal;
}



//<addinforecipients>
//   <source>web-browser</source>
//   <emailid>kamal.nandan@gmail.com</emailid>
//   <tokenid>123</tokenid>
//   <infoid>1234</infoid>
//   <recipients>
//      <recipient name="kn" emailid="hello.kr.dev@gmail.com" phone="9971295647" address="1260, sec 29, noida" relationship="brother"/>
//   </recipients>
//</addinforecipients>
OMElement* ImaService::addInfoRecipients(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::addInfoRecipients called");
	
	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_ADDINFORECIPIENTSRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);

	std::string strSource, strEmailId, strTokenId, strInfoId;
	std::vector<INFO_RECIPIENT*> recipientVector;
	
	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "USer %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "USer %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		OMElement* pInfoIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_INFOID), NULL);
		if(!pInfoIdEle) {
			LOG(debug, __FILE__, __LINE__, "Info Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Info Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Info ID element found");
			strInfoId = getElementText(pInfoIdEle);
		}

		if(status == 0) {
			OMElement* pRecipientsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_RECIPIENTS), NULL);
			if(!pRecipientsEle) {
				LOG(debug, __FILE__, __LINE__, "Recipeints element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Recipients element missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Recipients element found");
				if(!extractInfoRecipients(pRecipientsEle, recipientVector)) {
					createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Not a single recipient available for the info");
					LOG(debug, __FILE__, __LINE__, "Not a single recipient available for the info");
					status = -1;
				}
			}
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		long infoId = atol(strInfoId.c_str());
		char* pJsonReqPkt = jsonAssembler.assembleAddInfoRecipientsPkt(strSource, strEmailId, infoId, recipientVector);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result == 0) {
					if(!assembleAddInfoRecipientsXmlResponse(respPkt, recipientVector, pRespElement)) {
						createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Something went wrong while adding recipient element");
						status = -1;
					}
				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for addInfoRecipient, received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make add info recipient request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}
	
	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}


OMElement* ImaService::deleteInfoRecipient(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::deleteInfoRecipient called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_DELETEINFORECIPIENTRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	std::string strSource, strEmailId, strTokenId;
	long infoId = 0, recipientId = 0;

	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "USer %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "USer %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		if(status == 0) {
			OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
			if(!pDetailsEle) {
				LOG(debug, __FILE__, __LINE__, "Details element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Details missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Details element found");
				std::string strInfoId, strRecipientId;
				strInfoId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_INFOID, NULL));
				strRecipientId = getElementText(pDetailsEle->getChildElement(IMA_SERVICE_XML_TAG_RECIPIENTID, NULL));
				if((strInfoId.length() <=0) || (strRecipientId.length() <= 0)) {
					LOG(debug, __FILE__, __LINE__, "infoid and recipientid field can't be empty");
					status = -1;
					createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "infoid and recipientid field can't be empty");
				} else {
					infoId = atol(strInfoId.c_str());
					recipientId = atol(strRecipientId.c_str());
					if((infoId <= 0) || (recipientId <=0)) {
						LOG(debug, __FILE__, __LINE__, "infoid and recipientid fields have to be positive number");
						status = -1;
						createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "infoid and recipientid fields have to be positive number");
					}
				}
			}
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleDeleteInfoRecipientPkt(strSource, strEmailId, infoId, recipientId);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
			  LOG(debug, __FILE__, __LINE__, "getJsonIntValue returned non-zero");	
				if(result == 0) {
					std::string strInfoId;
					getJsonStringValue(respPkt, IMA_JSON_KEY_ID, strInfoId);
					OMElement* pDetailsElement = new OMElement(IMA_SERVICE_XML_TAG_DETAILS);
					bRetVal = pRespElement->addChild(pDetailsElement);	
					createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_INFOID, strInfoId.c_str());
				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for deleteInfoRecipient received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make deleteInfoRecipient request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

int ImaService::extractRecipientDetails(OMElement* pDetailsEle, std::string& strName, std::string& strEmailId, std::string& strPhone, std::string& strAddress, std::string& strRelationship, OMElement* pMessagesElement) {
	LOG(debug, __FILE__, __LINE__, "ImaService::extractRecipientDetails called");

	int retVal = 0;

	OMElement* pTempEle = pDetailsEle->getChildElement(string(IMA_SERVICE_XML_TAG_NAME), NULL);
	if(pTempEle) {
		strName = getElementText(pTempEle);
		if(strName.length() <= 0) {
			LOG(debug, __FILE__, __LINE__, "Recipient name is empty");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Recipient name is an empty string");
			retVal = -1;
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "Recipient name element missing");
		createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Recipient name element missing");
		retVal = -1;
	}
				
	pTempEle = pDetailsEle->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
	if(pTempEle) {
		strEmailId = getElementText(pTempEle);
	} else {
		LOG(debug, __FILE__, __LINE__, "Recipient email id element missing");
	}

	pTempEle = pDetailsEle->getChildElement(string(IMA_SERVICE_XML_TAG_ADDRESS), NULL);
	if(pTempEle) {
		strAddress = getElementText(pTempEle);
	} else {
		LOG(debug, __FILE__, __LINE__, "Recipient address element missing");
	}
	
	pTempEle = pDetailsEle->getChildElement(string(IMA_SERVICE_XML_TAG_PHONE), NULL);
	if(pTempEle) {
		strPhone = getElementText(pTempEle);
	} else {
		LOG(debug, __FILE__, __LINE__, "Recipient phone element missing");
	}

	pTempEle = pDetailsEle->getChildElement(string(IMA_SERVICE_XML_TAG_RELATION_WITH_OWNER), NULL);
	if(pTempEle) {
		strRelationship = getElementText(pTempEle);
	} else {
		LOG(debug, __FILE__, __LINE__, "Recipient relationship with owner element missing");
	}

	if((strName.length() <= 0)
		|| ((strEmailId.length() <= 0)
			&& (strAddress.length() <= 0)
			&&	(strPhone.length() <= 0)
			&& (strRelationship.length() <=0))) {
		LOG(debug, __FILE__, __LINE__, "Recipient name alongwith atleast one contact details are required");
		createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Recipient name alongwith atleast one contact details are required");
		retVal = -1;
	}

	return retVal;
}

//<addrecipient>
//   <emailid>kn@gmail.com</emailid>
//   <tokenid>123</tokenid>
//   <details>
//      <name> kn</name>
//      <emailid>dss@gmail.com</emailid>
//      <phone>9971295647</phone>
//      <address>sdkadm</address>
//      <relationship>dsfkf</relationship>
//   </details>
//</addrecipient>
OMElement* ImaService::addRecipient(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::addRecipient called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_ADDRECIPIENTRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	std::string strSource, strEmailId, strTokenId;
	long recipientId;
	std::string strRecipientName, strRecipientEmailId, strRecipientPhone, strRecipientAddress, strRelationshipWithOwner;
	
	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "User %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "User %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		if(status == 0) {
			OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
			if(!pDetailsEle) {
				LOG(debug, __FILE__, __LINE__, "Details element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Recipient details missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Details element found");
				if(0 != extractRecipientDetails(pDetailsEle, strRecipientName, strRecipientEmailId, strRecipientPhone, strRecipientAddress, strRelationshipWithOwner, pMessagesElement)) {
					LOG(debug, __FILE__, __LINE__, "Required details for adding recipient missing");
					status = -1;
					createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Required details for adding recipient details missing");
				}
			}
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleAddRecipientPkt(strSource, strEmailId, strRecipientName, strRecipientEmailId, strRecipientPhone, strRecipientAddress, strRelationshipWithOwner);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result == 0) {
					std::string strRecipientId;
					getJsonStringValue(respPkt, IMA_JSON_KEY_ID, strRecipientId);
					//OMElement* pDetailsElement = new OMElement(IMA_SERVICE_XML_TAG_DETAILS);
					//bRetVal = pRespElement->addChild(pDetailsElement);
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_RECIPIENTID, strRecipientId.c_str());
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_NAME, strRecipientName.c_str());
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_EMAILID, strRecipientEmailId.c_str());
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_PHONE, strRecipientPhone.c_str());
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_ADDRESS, strRecipientAddress.c_str());
					//createChildElement(pDetailsElement, IMA_SERVICE_XML_TAG_RELATION_WITH_OWNER, strRelationshipWithOwner.c_str());
					createChildElement(pRespElement, IMA_SERVICE_XML_TAG_RECIPIENTID, strRecipientId.c_str());

				} else {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for addRecipient received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make addRecipient request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

//<updaterecipient>
//   <emailid>kn@gmail.com</emailid>
//   <tokenid>123</tokenid>
//   <recipientid>123</recipientid>
//   <details>
//      <name> kn</name>
//      <emailid>dss@gmail.com</emailid>
//      <phone>9971295647</phone>
//      <address>sdkadm</address>
//      <relationship>dsfkf</relationship>
//   </details>
//</updaterecipient>
OMElement* ImaService::updateRecipient(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::updateRecipient called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_UPDATERECIPIENTRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	std::string strSource, strEmailId, strTokenId;
	long recipientId;
	std::string strRecipientName, strRecipientEmailId, strRecipientPhone, strRecipientAddress, strRelationshipWithOwner;
	
	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "User %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "User %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		OMElement* pRecipientIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_RECIPIENTID), NULL);
		if(!pRecipientIdEle) {
			LOG(debug, __FILE__, __LINE__, "Recipient Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Recipient Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Recipient ID element found");
			std::string strRecipientId = getElementText(pRecipientIdEle);
			recipientId = atol(strRecipientId.c_str());
			if(recipientId <= 0) {
				LOG(debug, __FILE__, __LINE__, "Recipient ID cannot be <= 0");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Recipient ID cannot be <= 0");
			}
		}

		if(status == 0) {
			OMElement* pDetailsEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_DETAILS), NULL);
			if(!pDetailsEle) {
				LOG(debug, __FILE__, __LINE__, "Details element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Recipeint updated details missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Details element found");
				if(0 != extractRecipientDetails(pDetailsEle, strRecipientName, strRecipientEmailId, strRecipientPhone, strRecipientAddress, strRelationshipWithOwner, pMessagesElement)) {
					LOG(debug, __FILE__, __LINE__, "Required details for updating recipient details missing");
					status = -1;
					createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, "Required details for updating recipient details missing");
				}
			}
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleUpdateRecipientPkt(strSource, strEmailId, recipientId, strRecipientName, strRecipientEmailId, strRecipientPhone, strRecipientAddress, strRelationshipWithOwner);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result != 0) {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				} else {
					std::string strRecId;
					getJsonStringValue(respPkt, IMA_JSON_KEY_ID, strRecId);
					createChildElement(pRespElement, IMA_SERVICE_XML_TAG_RECIPIENTID, strRecId.c_str());
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for updateRecipient received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make updateRecipient request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;
}

//<deleterecipient>
//   <emailid>kn@gmail.com</emailid>
//   <tokenid>123</tokenid>
//	 <recipientid>456</recipientid>
//</deleterecipient>

OMElement* ImaService::deleteRecipient(OMElement* inMsg, MessageContext* pMsgCtx) {
	LOG(debug, __FILE__, __LINE__, "ImaService::deleteRecipient called");

	int status = 0;
	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_DELETERECIPIENTRESPONSE);
	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
	bool bRetVal = pRespElement->addChild(pMessagesElement);	

	std::string strSource, strEmailId, strTokenId;
	long recipientId;
	
	if(inMsg) {
		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
		if(!pSourceEle) {
			LOG(debug, __FILE__, __LINE__, "Source element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Source element found");
			strSource = getElementText(pSourceEle);
		}

		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
		if(!pEmailIdEle) {
			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Email ID element found");
			strEmailId = getElementText(pEmailIdEle);
		}
		
		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
		if(!pTokenIdEle) {
			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
			status = -1;
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
		} else {
			LOG(debug, __FILE__, __LINE__, "Token ID element found");
			strTokenId = getElementText(pTokenIdEle);
		}

		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
			char log_msg[1024];
			sprintf(log_msg, "User %s is NOT active", strEmailId.c_str());
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(debug, __FILE__, __LINE__, log_msg);
			status = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "User %s is active", strEmailId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		if(status == 0) {
			OMElement* pRecipientIddEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_RECIPIENTID), NULL);
			if(!pRecipientIddEle) {
				LOG(debug, __FILE__, __LINE__, "Recipient Id element missing");
				status = -1;
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Recipient Id missing");
			} else {
				LOG(debug, __FILE__, __LINE__, "Recipient ID element found");
				std::string strRecipientId = getElementText(pRecipientIddEle);
				recipientId = atol(strRecipientId.c_str());
			}
		}
	}

	if(status == 0) {
		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
		ImaJsonAssembler jsonAssembler;
		char* pJsonReqPkt = jsonAssembler.assembleDeleteRecipientPkt(strSource, strEmailId, recipientId);
		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
		string respPkt;
		long result;
		printf("json request pkt is %s\n", pJsonReqPkt);
		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
			char log_msg[1024];
			snprintf(log_msg, 1024, "json response is %s", respPkt.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
			printf("%s", log_msg);
			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
				if(result != 0) {
					status = -1;
					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
				}
			} else {
				status = -1;
				char log_msg[1024];
				sprintf(log_msg, "The JSON response for deleteRecipient received from the appserver is not correct");
				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
				LOG(error, __FILE__, __LINE__, log_msg);
			}
		} else {
			status = -1;
			char log_msg[1024];
			sprintf(log_msg, "Failed to make deleteRecipient request because of some communication error");
			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
			LOG(error, __FILE__, __LINE__, log_msg);
		}
		free(pJsonReqPkt);
	}

	char strStatus[8];
	sprintf(strStatus, "%d", status);	

	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
	pRespElement->addAttribute(pAttrib);

	return pRespElement;

}

//<fetchuserdata>
//        <emailid>kamal.nandan@gmail.com</emailid>
//        <tokenid>jkfjsdfjj</tokenid>
//</fetchuserdata>
//OMElement* ImaService::fetchUserData(OMElement* inMsg, MessageContext* pMsgCtx) {
//	LOG(debug, __FILE__, __LINE__, "ImaService::fetchUserData called");
//
//	int status = 0;
//	OMElement* pRespElement = new OMElement(IMA_SERVICE_XML_TAG_FETCHUSERDATARESPONSE);
//	OMElement* pMessagesElement = new OMElement(IMA_SERVICE_XML_TAG_MESSAGES);
//	bool bRetVal = pRespElement->addChild(pMessagesElement);	
//
//	std::string strSource, strEmailId, strTokenId;
//	
//	if(inMsg) {
//		OMElement* pSourceEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_SOURCE), NULL);
//		if(!pSourceEle) {
//			LOG(debug, __FILE__, __LINE__, "Source element missing");
//			status = -1;
//			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Source element missing");
//		} else {
//			LOG(debug, __FILE__, __LINE__, "Source element found");
//			strSource = getElementText(pSourceEle);
//		}
//
//		OMElement* pEmailIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_EMAILID), NULL);
//		if(!pEmailIdEle) {
//			LOG(debug, __FILE__, __LINE__, "EmailId element missing");
//			status = -1;
//			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "EMail Id missing");
//		} else {
//			LOG(debug, __FILE__, __LINE__, "Email ID element found");
//			strEmailId = getElementText(pEmailIdEle);
//		}
//		
//		OMElement* pTokenIdEle = inMsg->getChildElement(string(IMA_SERVICE_XML_TAG_TOKENID), NULL);
//		if(!pTokenIdEle) {
//			LOG(debug, __FILE__, __LINE__, "Token Id element missing");
//			status = -1;
//			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE,  "Token Id missing");
//		} else {
//			LOG(debug, __FILE__, __LINE__, "Token ID element found");
//			strTokenId = getElementText(pTokenIdEle);
//		}
//
//		if(!ImaActiveUsers::getInstance()->isUserActive(strEmailId, strTokenId)){
//			char log_msg[1024];
//			sprintf(log_msg, "User %s is NOT active", strEmailId.c_str());
//			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
//			LOG(debug, __FILE__, __LINE__, log_msg);
//			status = -1;
//		} else {
//			char log_msg[1024];
//			sprintf(log_msg, "User %s is active", strEmailId.c_str());
//			LOG(debug, __FILE__, __LINE__, log_msg);
//		}
//	}
//
//	if(status == 0) {
//		LOG(debug, __FILE__, __LINE__, "Status is 0 i.e. Success");
//		ImaJsonAssembler jsonAssembler;
//		char* pJsonReqPkt = jsonAssembler.assembleFetchUserDataPkt(strSource, strEmailId);
//		ImaTcpRequestMaker reqMaker(gpConf->mAppServerHost, gpConf->mAppServerPort);
//		string respPkt;
//		long result;
//		printf("json request pkt is %s\n", pJsonReqPkt);
//		if(0 == reqMaker.makeRequest(pJsonReqPkt, respPkt)) {
//			char log_msg[1024];
//			sprintf(log_msg, "json response is %s", respPkt.c_str());
//			LOG(debug, __FILE__, __LINE__, log_msg);
//			printf("%s", log_msg);
//			if(0 == getJsonIntValue(respPkt, IMA_JSON_KEY_RESULT, result)) {
//				if(result != 0) {
//					status = -1;
//					extractMessagesFromJsonAndAppendIntoXmlEle(respPkt, pMessagesElement);
//				} else {
//					
//				}
//			} else {
//				status = -1;
//				char log_msg[1024];
//				sprintf(log_msg, "The JSON response for fetchUserData received from the appserver is not correct");
//				createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
//				LOG(error, __FILE__, __LINE__, log_msg);
//			}
//		} else {
//			status = -1;
//			char log_msg[1024];
//			sprintf(log_msg, "Failed to make addRecipient request because of some communication error");
//			createChildElement(pMessagesElement, IMA_SERVICE_XML_TAG_MESSAGE, log_msg);
//			LOG(error, __FILE__, __LINE__, log_msg);
//		}
//		free(pJsonReqPkt);
//	}
//
//	char strStatus[8];
//	sprintf(strStatus, "%d", status);	
//
//	OMNamespace* ns = new OMNamespace(IMA_SERVICE_XML_NS, IMA_SERVICE_XML_NS_PREFIX);
//	OMAttribute* pAttrib = new OMAttribute(string(IMA_SERVICE_XML_ATTRIB_STATUS), string(strStatus), ns);
//	pRespElement->addAttribute(pAttrib);
//
//	return pRespElement;
//}
